<template>
  <!-- 修改资料 -->
  <div>
    <!-- 内容主题区域 -->
    <el-form
      label-width="120px"
      :model="form"
      label-position="right"
      ref="formRef"
      :rules="formRules"
    >
      <el-row>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="数据库表名" prop="number">
            <el-input
              style="width: 70%"
              v-model="form.number"
              placeholder="请输入数据库表名"
            ></el-input
            >&nbsp;
            <el-button style="width: 20%" type="primary" @click="search"
              >查询表字段</el-button
            >
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="表描述" prop="name">
            <el-input v-model="form.name" placeholder="请输入表描述"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="映射实体名" prop="entityName">
            <el-input v-model="form.entityName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="表主键" prop="primaryFieldName">
            <el-input v-model="form.primaryFieldName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="记录唯一字段" prop="uniqueFields">
            <el-input v-model="form.uniqueFields" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="是否自增型主键" prop="sautoIncreasePrimary">
            <el-switch v-model="form.autoIncreasePrimary"> </el-switch>
          </el-form-item>
        </el-col>
      </el-row>
      <sc-title title="表字段列表"></sc-title>
      <sc-form-table
        v-model="dataList"
        :addTemplate="addTemplate"
        edit
        placeholder="暂无数据"
        @delete="rowDelete"
      >
        <template #tableContent>
          <el-table-column prop="number" label="字段名" width="200">
            <template #default="scope">
              <span v-if="scope.row.operationType == ''">{{
                scope.row.number
              }}</span>
              <el-input
                v-else
                v-model="scope.row.number"
                placeholder="请输入数据库表名"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="字段描述" width="200">
            <template #default="scope">
              <span v-if="scope.row.operationType == ''">{{
                scope.row.name
              }}</span>
              <el-input
                v-else
                v-model="scope.row.name"
                placeholder="请输入名称"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="字段类型" width="110">
            <template #default="scope">
              <span v-if="scope.row.operationType == ''">{{
                scope.row.fieldTypeName
              }}</span>
              <el-select
                v-else
                v-model="scope.row.fieldType"
                clearable
                placeholder="请选择："
              >
                <el-option
                  v-for="item in configData.fieldTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="refMainTableName" label="关联主表名">
            <template #default="scope">
              <span v-if="scope.row.operationType == ''">{{
                scope.row.refMainTableName
              }}</span>
              <el-input v-else v-model="scope.row.refMainTableName"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="refMainTableFieldName" label="关联主表字段">
            <template #default="scope">
              <span v-if="scope.row.operationType == ''">{{
                scope.row.refMainTableFieldName
              }}</span>
              <el-input
                v-else
                v-model="scope.row.refMainTableFieldName"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="lng" label="字段长度">
            <template #default="scope">
              <span v-if="scope.row.operationType == ''">{{
                scope.row.lng
              }}</span>
              <el-input v-else v-model="scope.row.lng"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="scale" label="小数位数">
            <template #default="scope">
              <span v-if="scope.row.operationType == ''">{{
                scope.row.scale
              }}</span>
              <el-input v-else v-model="scope.row.scale"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="nullable" label="是否可为空">
            <template #default="scope">
              <el-switch
                v-model="scope.row.nullable"
                :disabled="scope.row.operationType == ''"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="autoIncrement" label="是否自增型">
            <template #default="scope">
              <el-switch
                v-model="scope.row.autoIncrement"
                :disabled="scope.row.operationType == ''"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="autoIncrement" label="是否导入">
            <template #default="scope">
              <el-switch v-model="scope.row.importFlag" />
            </template>
          </el-table-column>
          <el-table-column prop="defaultValue" label="默认值">
            <template #default="scope">
              <span v-if="scope.row.operationType == ''">{{
                scope.row.defaultValue
              }}</span>
              <el-input v-else v-model="scope.row.defaultValue"></el-input>
            </template>
          </el-table-column>
        </template>
      </sc-form-table>
    </el-form>
    <div class="button-position">
      <el-button @click="updateVisible">取 消</el-button>
      <el-button type="primary" @click="update(formRef)">确 定</el-button>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { ElNotification } from "element-plus";
import { Check, Close } from "@element-plus/icons-vue";
import Api from "@/api/model/meta";
const emit = defineEmits(["updatetable"]);
const props = defineProps({
  paramsData: {
    type: Object,
    defalut: {},
  },
});
// 添加用户数据的对象
const form = ref({});
const configData = ref({});
const addTemplate = ref({});
const dataList = ref([]);
const deleteList = ref([]);
const formRef = ref();

onMounted(() => {
  getInit();
});

const getInit = async () => {
  let formRes = await Api.table.detail.post(props.paramsData);
  if (formRes.code == 200) {
    form.value = formRes.data;
    configData.value = formRes.configData;
    addTemplate.value = formRes.dtl;
    dataList.value = formRes.dataList;
  }
};
const search = async () => {
  if (form.value.number) {
    let param = {
      id: form.value.id,
      name: form.value.number,
    };
    let res = await Api.table.getTableSchema.post(param);
    if (res.code == 200) {
      dataList.value = res.dataList.filter((ele) => {
        return ele.operationType !== "delete";
      });
      deleteList.value = res.dataList.filter((ele) => {
        return ele.operationType === "delete";
      });
      form.value.primaryFieldName = res.primaryKey;
      form.value.entityName = res.entityName;
    }
  } else {
    ElNotification.info({
      title: "",
      message: "请输入数据库表名！",
      duration: 2000,
    });
  }
};
const rowDelete = (val) => {
  deleteList.value.push(val);
};

const restAddFormData = () => {
  formRef.value.resetFields();
};

const updateVisible = (val) => {
  emit("updatetable");
};

// 上传
const update = async (formRef) => {
  await formRef.validate((valid, fields) => {
    if (valid) {
      updateData();
    }
  });
};

const updateData = async () => {
  let params = {
    bean: form.value,
    dataList: dataList.value.concat(deleteList.value),
  };
  console.log(params);
  let res = await Api.table.update.post(params);
  if (res.code == 200) {
    restAddFormData();
    ElNotification.success({
      title: "",
      message: "更新成功！",
      duration: 2000,
    });
    emit("updatetable", true);
  }
};
</script>

<style lang="less" scoped>
.el-dialog__wrapper {
  pointer-events: none;
}

.el-dialog {
  pointer-events: auto;
}
</style>

